package com.example.demo.controller.admin;

import com.example.demo.helper.CONSTANT;
import com.example.demo.helper.Function;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.InventoryMapper;
import com.example.demo.model.Goods;
import com.example.demo.model.Inventory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/goods")
public class GoodsController {
    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @GetMapping("/list")
    @ResponseBody
    public Map<String,Object> list(HttpServletRequest httpServletRequest) {
        //Model model = null;
        String typeidStr = httpServletRequest.getParameter("typeid");
        Integer typeid = Integer.valueOf(typeidStr);
        List<Goods> list = goodsMapper.list(typeid);
        for (Goods goods : list) {
            if (goods.getCount() < 5) {
                goods.setIsshortage(1);
            } else {
                goods.setIsshortage(0);
            }
        }
        /**model.addAttribute("list", list);
        model.addAttribute("typeid", typeid);*/
        Boolean state = true;
        Map<String, Object> map = new HashMap<>();
        map.put("status", CONSTANT.OK);
        map.put("list",list);
        map.put("typeid",typeid);
        return map;
    }

    @GetMapping("/add")
    @ResponseBody
    public Map<String,Object> addPage(HttpServletRequest httpServletRequest) {
        /**model.addAttribute("goods", new Goods());
        model.addAttribute("typeid", typeid);*/
        String typeidStr = httpServletRequest.getParameter("typeid");
        Integer typeid = Integer.valueOf(typeidStr);
        Map<String, Object> res = new HashMap<>();
        res.put("status", CONSTANT.OK);
        res.put("list","");
        res.put("typeid", typeid);
        return res;
    }

    @PostMapping("/add")
    @ResponseBody
    @Transactional
    public Map<String,Object> add(HttpServletRequest httpServletRequest, @RequestBody Goods goods) {
        String typeidStr = httpServletRequest.getParameter("typeid");
        Integer typeid = Integer.valueOf(typeidStr);
        goods.setTypeid(typeid);
        goods.setStatus(1);
        Date starttime = Function.getSqlDate();
        goods.setStarttime(starttime);
        goodsMapper.insert(goods);
        Inventory inventory = new Inventory();
        inventory.setGid(goods.getGid());
        inventory.setUid(1);
        inventory.setInvtime(starttime);
        inventory.setIssale(0);
        int count = goods.getCount();
        int price = goods.getPrice();
        inventory.setCount(count);
        inventory.setUnitprice(price);
        inventory.setTotalprice(count * price);
        inventoryMapper.insert(inventory);
        Map<String, Object> res = new HashMap<>();
        res.put("status", CONSTANT.OK);
        res.put("list","添加成功");
        res.put("typeid", typeid);
        return res;
    }

    @GetMapping("/stock")
    @ResponseBody
    public Map<String, Object> stockPage(HttpServletRequest httpServletRequest) {
        String gidStr = httpServletRequest.getParameter("gid");
        Integer gid = Integer.valueOf(gidStr);
        Goods goods = goodsMapper.findByGid(gid);
        //model.addAttribute("goods", goods);
        Map<String, Object> res = new HashMap<>();
        res.put("status", CONSTANT.OK);
        res.put("list","添加成功");
        res.put("typeid", gid);
        return res;
    }

    @PostMapping("/stock")
    @ResponseBody
    @Transactional
    public Map<String, Object> stock(HttpServletRequest httpServletRequest, @RequestParam(value = "count") int count) {
        String gidStr = httpServletRequest.getParameter("gid");
        Integer gid = Integer.valueOf(gidStr);
        Goods goods = goodsMapper.findByGid(gid);
        goods.setCount(goods.getCount() + count);
        goodsMapper.updateCount(goods);
        Inventory inventory = new Inventory();
        inventory.setGid(goods.getGid());
        inventory.setUid(1);
        inventory.setInvtime(Function.getSqlDate());
        inventory.setIssale(0);
        inventory.setCount(count);
        int price = goods.getPrice();
        inventory.setUnitprice(price);
        inventory.setTotalprice(count * price);
        inventoryMapper.insert(inventory);
        Map<String, Object> res = new HashMap<>();
        res.put("status", CONSTANT.OK);
        res.put("list","");
        res.put("typeid", gid);
        return res;
    }
}
